
package com.neuedu.aspectj.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {

    @Pointcut("execution(* com.neuedu.jdk.*.*(..))")
    private void myPointCut(){}
    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint){
        System.out.print("前置通知：检查权限。。。");
        System.out.print("目标类："+joinPoint.getTarget());
        System.out.println(",被植入增强的方法："+joinPoint.getSignature().getName()); 
    }
    @AfterReturning("myPointCut()")
    public void myAfterReturning(JoinPoint joinPoint){
        System.out.print("后置通知：记录日志。。。");
        System.out.println("被植入增强处理的目标方法类："+joinPoint.getSignature().getName());    }
    @Around("myPointCut()")
    public Object myAround(ProceedingJoinPoint pjp)throws Throwable{
        System.out.println("环绕开始：执行目标之前，模拟开启事务。。。");
        Object obj=pjp.proceed();
        System.out.println("环绕结束：执行目标之后，模拟关闭事务。。。");
        return obj;    }
    @AfterThrowing(value = "myPointCut()", throwing = "e")
    public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("异常通知：出错了。。"+e.getMessage());    }
    @After("myPointCut()")
    public void myAfter(){
        System.out.println("最终通知：模拟结束适放资源。。");    }
}
